@echo off
REM ========================================
REM n8ncrm Backend Server Startup Script
REM ========================================

echo.
echo ========================================
echo Starting n8ncrm Backend Server
echo ========================================
echo.

REM Change to the project root directory
cd /d "%~dp0"

REM Resolve Python interpreter (MUST use virtual environment)
set "PYTHON_EXE="
if exist "backend\venv\Scripts\python.exe" (
    set "PYTHON_EXE=backend\venv\Scripts\python.exe"
) else if exist ".venv\Scripts\python.exe" (
    set "PYTHON_EXE=.venv\Scripts\python.exe"
) else (
    echo [ERROR] Virtual environment not found!
    echo.
    echo Please create virtual environment first:
    echo   python -m venv backend\venv
    echo   backend\venv\Scripts\activate.bat
    echo   pip install -r requirements.txt
    echo.
    pause
    exit /b 1
)

REM Check if .env file exists
if not exist ".env" (
    echo [WARNING] .env file not found!
    echo Please copy .env.example to .env and configure it.
    pause
    exit /b 1
)

REM Display configuration
echo [INFO] Project Directory: %CD%
echo [INFO] Python (Virtual Environment): %PYTHON_EXE%
echo [INFO] Starting Backend via scripts\start_backend.py ...
echo [INFO] Server will run on: http://127.0.0.1:8000
echo [INFO] API Documentation: http://127.0.0.1:8000/docs
echo [INFO] Press CTRL+C to stop the server
echo.
echo ========================================
echo.

REM Delegate to Python launcher script
%PYTHON_EXE% scripts\start_backend.py

REM If server stops, pause to show any error messages
echo.
echo ========================================
echo Server stopped
echo ========================================
pause
